package com.dubbo.order.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.common.dtos.PageResponseResult;
import com.dubbo.common.dtos.ResponseResult;
import com.dubbo.model.order.dtos.PageServiceDTO;
import com.dubbo.model.order.dtos.ServiceDTO;
import com.dubbo.model.order.dtos.ServiceOrderDTO;

import com.dubbo.model.order.pojos.ServiceOperate;
import com.dubbo.model.order.pojos.ServiceOrder;
import com.dubbo.model.order.pojos.ServiceProject;
import com.dubbo.order.ServiceOrderService;


import com.dubbo.order.mapper.ServiceOperateMapper;
import com.dubbo.order.mapper.ServiceOrderMapper;
import com.dubbo.order.mapper.ServiceProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.List;

@Service(timeout = 6000000, retries = 0)
@Component
public class ServiceOrderServiceImpl implements ServiceOrderService {


    @Autowired
    private ServiceOrderMapper orderMapper;
    @Autowired
    private ServiceOperateMapper operateMapper;
    @Autowired
    private ServiceProjectMapper projectMapper;

    @Override
    public ResponseResult<List<ServiceOrder>> paretsOrderList(PageServiceDTO dto) {
        //设置页码大小
        dto.checkParam();
        Integer dtoPage = dto.getPage();
        dto.setPage((dtoPage - 1) * dto.getSize());

        List<ServiceOrder> serviceOrders = orderMapper.paretsOrderList(dto);
        Integer count = orderMapper.count(dto.getEnable());

        PageResponseResult pageResponseResult = new PageResponseResult(dtoPage,dto.getSize(),count);
        pageResponseResult.setData(serviceOrders);
        return pageResponseResult;
    }

    @Override
    public ResponseResult<ServiceDTO> findById(Long id) {
        ServiceOrder serviceOrder = orderMapper.findById(id);
        if (serviceOrder == null){
            return null;
        }
        List<ServiceOperate> operateList = operateMapper.findByOrderId(serviceOrder.getId());
        List<ServiceProject> projectList = projectMapper.findByOrderId(serviceOrder.getId());

        ServiceDTO serviceDTO = new ServiceDTO();
        serviceDTO.setServiceOrder(serviceOrder);

        if (operateList != null){
            serviceDTO.setServiceOperate(operateList);
        }

        if (projectList != null){
            serviceDTO.setServiceProject(projectList);

            double totalMoney = 0;
            for (ServiceProject serviceProject : projectList) {
                totalMoney += serviceProject.getMoney();
            }
            serviceDTO.setTotalMoney(totalMoney);
        }

        return ResponseResult.okResult(serviceDTO);
    }

    @Override
    public ResponseResult<List<ServiceOrder>> selectByPage(ServiceOrderDTO dto) {
        //设置页码大小
        dto.checkParam();
        Integer dtoPage = dto.getPage();
        dto.setPage((dtoPage - 1) * dto.getSize());

        List<ServiceOrder> serviceOrderList = orderMapper.selectByPage(dto);
        Integer count = orderMapper.selectCount(dto);

        PageResponseResult pageResponseResult = new PageResponseResult(dtoPage,dto.getSize(),count);
        pageResponseResult.setData(serviceOrderList);
        return pageResponseResult;
    }


    @Override
    public ResponseResult closeOrder(Long id) {
        //先判断状态
        ServiceOrder serviceOrder = orderMapper.findById(id);
        if (serviceOrder == null){
            return ResponseResult.okResult(400,"没有该订单");
        }
        if (serviceOrder.getEnable() !=1){
            //无需关闭
            return ResponseResult.okResult(200,"无需关闭");
        }

        orderMapper.closeOrder(id);

       return ResponseResult.okResult(200,"订单关闭成功");
    }
}
